package com.example.wechatdaka.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wechatdaka.entity.Menu;
import com.example.wechatdaka.service.MenuService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
public class MenuIdUtil {
    public static void main(String[] args) {
        String storeMenuIds = storeMenuIds("[[\"1\"],[\"1\",\"12\"],[\"1\",\"2\"],[\"6\"],[\"6\",\"7\"],[\"6\",\"8\"]]");
        System.out.println(storeMenuIds);
    }
    @Autowired
    private MenuService menuService;
    private static MenuIdUtil menuIdUtil;
    @PostConstruct
    public void init(){
        menuIdUtil = this;
        menuIdUtil.menuService=this.menuService;

    }

    public static String storeMenuIds(String menuIds){
        System.out.println("menuIds"+menuIds);
        menuIds = StringEscapeUtils.unescapeHtml4(menuIds);//2、处理最外层 双引号 的乱码
        menuIds = menuIds .substring(1,menuIds .length()-1);//3、去掉  [  和  ],结果： “1”，“2”，“3”
        menuIds = menuIds.replaceAll("\\[", "");
        menuIds = menuIds.replaceAll("\\]", "");
        menuIds = menuIds.replaceAll("\"", "");
        String[] arr=menuIds.split(",");
        System.out.println(menuIds.toString());
        List<String> menuIdList= Arrays.asList(arr);
        System.out.println(menuIdList.toString());
        QueryWrapper<Menu> menuQueryWrapper=new QueryWrapper<>();
        menuQueryWrapper.isNull("lxFather").orderByAsc("lxOrder");
        List<Menu> menuFatherList = menuIdUtil.menuService.list(menuQueryWrapper);
        List<String> menuFatherIdList=new ArrayList<>();
        List<String> newMenuIdList=new ArrayList<>();
        for (int i = 0; i < menuFatherList.size(); i++) {
            menuFatherIdList.add(menuFatherList.get(i).getId());
        }
        for (int i = 0; i <menuIdList.size(); i++) {
            if (menuFatherIdList.contains(menuIdList.get(i))){
                continue;
            }else {
                newMenuIdList.add(menuIdList.get(i));
            }

        }
        System.out.println("newMenuIdList"+newMenuIdList);
        String storeMenuIds=newMenuIdList.toString();
        return storeMenuIds;
    }

//    public static String queryMenuIds{
//        String queryMenuIds="[12, 2, 3, 7, 8]";
//        queryMenuIds = queryMenuIds.replaceAll("\\[", "");
//        queryMenuIds = queryMenuIds.replaceAll("\\]", "");
//        String [] arr=queryMenuIds.split(",");
//        List<String> menuIdList= Arrays.asList(arr);
//
//    }


}
